<html>
<head><meta charset="utf-8"><title>decl macro syntax · t-lang · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/index.html">t-lang</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/decl.20macro.20syntax.html">decl macro syntax</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="193805459"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/decl%20macro%20syntax/near/193805459" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Simon Sapin <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/decl.20macro.20syntax.html#193805459">(Apr 13 2020 at 18:44)</a>:</h4>
<p><span class="user-mention" data-user-id="119009">@eddyb</span> I don’t understand <a href="https://github.com/rust-lang/rust/pull/70817#issuecomment-613020732" title="https://github.com/rust-lang/rust/pull/70817#issuecomment-613020732">https://github.com/rust-lang/rust/pull/70817#issuecomment-613020732</a> re how to use <code>macro</code> with multiple token-matching arms. <code>macro_rules</code> has another nesting level of <code>{…}</code></p>



<a name="193805888"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/decl%20macro%20syntax/near/193805888" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/decl.20macro.20syntax.html#193805888">(Apr 13 2020 at 18:48)</a>:</h4>
<p><span class="user-mention" data-user-id="219747">@Simon Sapin</span> you write <code>macro</code> instead of <code>macro_rules!</code> (and use <code>,</code> between the arms instead of <code>;</code>)</p>



<a name="193805900"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/decl%20macro%20syntax/near/193805900" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/decl.20macro.20syntax.html#193805900">(Apr 13 2020 at 18:48)</a>:</h4>
<p>the shorthand syntax is <em>optional</em></p>



<a name="193805906"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/decl%20macro%20syntax/near/193805906" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/decl.20macro.20syntax.html#193805906">(Apr 13 2020 at 18:48)</a>:</h4>
<p>as a nicety</p>



<a name="193806008"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/decl%20macro%20syntax/near/193806008" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/decl.20macro.20syntax.html#193806008">(Apr 13 2020 at 18:49)</a>:</h4>
<p><code>macro</code> is rebranded <code>macro_rules!</code>, it's not a different MBE system</p>



<a name="193806744"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/decl%20macro%20syntax/near/193806744" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Simon Sapin <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/decl.20macro.20syntax.html#193806744">(Apr 13 2020 at 18:54)</a>:</h4>
<p>MBE?</p>



<a name="193806858"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/decl%20macro%20syntax/near/193806858" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Simon Sapin <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/decl.20macro.20syntax.html#193806858">(Apr 13 2020 at 18:55)</a>:</h4>
<p>Ok, the part I was missing is that there’s a shorthand for single-arm when the macro name is followed by <code>(</code> instead of <code>{</code></p>



<a name="193806924"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/decl%20macro%20syntax/near/193806924" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Simon Sapin <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/decl.20macro.20syntax.html#193806924">(Apr 13 2020 at 18:56)</a>:</h4>
<p>The shorthand is the only form I’d seen in any example</p>



<a name="193831334"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/decl%20macro%20syntax/near/193831334" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/decl.20macro.20syntax.html#193831334">(Apr 13 2020 at 22:49)</a>:</h4>
<p><span class="user-mention silent" data-user-id="219747">Simon Sapin</span> <a href="#narrow/stream/213817-t-lang/topic/decl.20macro.20syntax/near/193806744" title="#narrow/stream/213817-t-lang/topic/decl.20macro.20syntax/near/193806744">said</a>:</p>
<blockquote>
<p>MBE?</p>
</blockquote>
<p>Macros By Example (I guess)</p>



<a name="193832267"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/decl%20macro%20syntax/near/193832267" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/decl.20macro.20syntax.html#193832267">(Apr 13 2020 at 23:00)</a>:</h4>
<p>yeah that's what the initialism stands for</p>



<a name="193835244"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/decl%20macro%20syntax/near/193835244" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/decl.20macro.20syntax.html#193835244">(Apr 13 2020 at 23:35)</a>:</h4>
<p><span class="user-mention" data-user-id="219747">@Simon Sapin</span> yeah, that doesn't help. everyone loves using that form when there's only one arm just because it looks pretty :P</p>



<a name="193835303"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/decl%20macro%20syntax/near/193835303" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/decl.20macro.20syntax.html#193835303">(Apr 13 2020 at 23:36)</a>:</h4>
<p>I remember being confused by the <code>,</code> vs <code>;</code> thing, a few years ago, and thinking <code>macro</code> doesn't support multiple arms</p>



<a name="193835316"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/decl%20macro%20syntax/near/193835316" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/decl.20macro.20syntax.html#193835316">(Apr 13 2020 at 23:36)</a>:</h4>
<p>might be good to replace all <code>macro_rules!</code> in the standard library with <code>macro</code>s with the attribute <span class="user-mention" data-user-id="123856">@Vadim Petrochenkov</span> mentioned</p>



<a name="193835329"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/decl%20macro%20syntax/near/193835329" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/decl.20macro.20syntax.html#193835329">(Apr 13 2020 at 23:36)</a>:</h4>
<p>and we can remove the attribute if/when we're sure it doesn't matter</p>



<a name="193835340"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/decl%20macro%20syntax/near/193835340" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/decl.20macro.20syntax.html#193835340">(Apr 13 2020 at 23:37)</a>:</h4>
<p>if nothing else we could try it as a test</p>



<a name="194321814"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/decl%20macro%20syntax/near/194321814" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> mark-i-m <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/decl.20macro.20syntax.html#194321814">(Apr 16 2020 at 15:26)</a>:</h4>
<p>We ought to document <code>macro</code> better... also, I still think it needs a real RFC</p>



<a name="194346631"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/decl%20macro%20syntax/near/194346631" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/decl.20macro.20syntax.html#194346631">(Apr 16 2020 at 18:21)</a>:</h4>
<p>Yeah. <span class="user-mention" data-user-id="123856">@Vadim Petrochenkov</span> pointed to the docs they wrote up on hygiene, and I was thinking it'd be great to dive into those and maybe have a design meeting or something where we try to really hash it out</p>



<a name="194354354"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/decl%20macro%20syntax/near/194354354" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Vadim Petrochenkov <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/decl.20macro.20syntax.html#194354354">(Apr 16 2020 at 19:19)</a>:</h4>
<p><code>macro</code> items have multiple components to figure out before stabilizing, syntactic and semantic:<br>
    - <code>Span::def_site</code>, pretty far away, requires implementing cross-crate hygiene <em>at least</em>, and then formalizing stuff in <code>hygiene.rs</code> more carefully, in application to type-relative paths in particular.<br>
    - Syntax of the macro's left side (macro "parameters"), requires major design work to figure out future-compatibility (currently done with FIRST and FOLLOW sets) and figuring out how to match or not match all arms at the same time.<br>
    - Syntax of the macro's right side (macro body), requires a syntax for hygiene opt-out perhaps, maybe simplifying the use of repetitions (<a href="https://github.com/rust-lang/rust/issues/61053#issuecomment-506950747" title="https://github.com/rust-lang/rust/issues/61053#issuecomment-506950747">https://github.com/rust-lang/rust/issues/61053#issuecomment-506950747</a>), but otherwise seems ok, it's just an arbitrary token stream.<br>
    - Surface syntax of the macro. The last year I almost wrote and RFC to set the top-level syntax <code>macro single_arm() {}</code> + <code>macro multiple_arms { (lhs1) {rhs1} (lhs2) {rhs2} }</code> in stone, but them recalled that people wanted the macros want to control what delimiters they are invoked with (e.g. restrict <code>vec![]</code> to only use square brackets), and that added more questions to the surface syntax, and I didn't write anything.</p>



<a name="194460378"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/decl%20macro%20syntax/near/194460378" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> mark-i-m <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/decl.20macro.20syntax.html#194460378">(Apr 17 2020 at 15:46)</a>:</h4>
<p><span class="user-mention" data-user-id="123856">@Vadim Petrochenkov</span> thanks for the update! It's probably worth reposting your comment on the tracking issue...</p>



<a name="194538470"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/decl%20macro%20syntax/near/194538470" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/decl.20macro.20syntax.html#194538470">(Apr 18 2020 at 10:55)</a>:</h4>
<p>we should move it to the header, even</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>